import java.lang.reflect.Field;

public class FanS {
    public static void main(String[] args) {
        Book2 book = new Book2();
// 获取动态类Book2
        Class class1 = book.getClass();
// 获取Book2类的所有成员
        Field[] de = class1.getDeclaredFields();
// 遍历所有的成员
        for (int i = 0 ;i < de.length;i++){
//            获取类中的成员变量
            Field field = de[i];
//            成员名称
            System.out.println("成员名称："+field.getName());
            Class finalType = field.getType();
            System.out.println("成员类型："+finalType);
            boolean isTrue = true;
            while (isTrue){
                try {
                    isTrue = false;
                    if (finalType.equals(int.class)){
                        System.out.println("利用setInt()修改");
                        field.setInt(book,100);
                    }else if (finalType.equals(float.class)) {
                        System.out.println("利用setFloat()修改");
                        field.setFloat(book, 1314.12f);
                    }else if (finalType.equals(boolean.class)){
                        System.out.println("利用setBoolean()修改");
                        field.setBoolean(book,true);
                    }else{
                        System.out.println("利用set()修改");
                        field.set(book,"java编程");

                    }
                    System.out.println("修改后成员值："+field.get(book));
                }catch (Exception e){
                    System.out.println("异常");
                    field.setAccessible(true);
                    isTrue=true;

                }
            }
            System.out.println("===============================\n");


        }

    }
}
 class Book2{
    String name;
    public int id;
    private float pirce;
    protected  boolean is;
}
